package cn.itcast.demo.controller;

import cn.itcast.demo.pojo.User;
import cn.itcast.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserController {

    //ResponseEntity ，在返回状态码同时，返回了数据，但是状态码每次都有，数据未必有

    @Autowired
    private UserService userService;

    @GetMapping("hello/{id}")
    public ResponseEntity<User> queryUserById(@PathVariable("id")Long id){
        //restful中状态码比返回内容重要，

        User user = this.userService.queryUserById(id);

        if (null==user){
            //204,null
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }
        //200,user
        return ResponseEntity.ok(user);
    }
}
